package com.clwl.supervise.superviseapp.controller.company;

import cn.hutool.core.lang.Assert;
import com.clwl.supervise.superviseapp.beans.company.CompanySearchParams;
import com.clwl.supervise.superviseapp.beans.company.SimpleCompany;
import com.clwl.supervise.superviseapp.beans.company.UpdateCompanyPortraitParams;
import com.clwl.supervise.superviseapp.entity.base.TcCompany;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.service.company.TcCompanyService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("company")
public class TcCompanyController {
    @Autowired
    TcCompanyService tcCompanyService;

    @GetMapping("getCompanyList")
    private ResultEntity<List<SimpleCompany>> getCompanyList(@RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize, CompanySearchParams params){
        List<SimpleCompany> dataList= tcCompanyService.selectCompanyList(pageNum,pageSize,params);
        return ResultEntity.success(dataList);
    }

    /**
     * 获取企业列表通过企业类型
     * @param pageNum
     * @param pageSize
     * @param typeCode  企业类型
     * @return
     */
    @GetMapping("getCompanyByType")
    private ResultEntity<List<Map<String,Object>>> getCompanyByType(@RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize,
                                                               String typeCode
                                                               ){
        List<Map<String,Object>> dataList= tcCompanyService.getCompanyByType(pageNum,pageSize,typeCode);
        return ResultEntity.success(dataList);
    }

    @GetMapping("getCompanyDetail")
    private ResultEntity<TcCompany> getCompanyDetail(@RequestParam(required = true) Long id ){
        TcCompany tcCompany= tcCompanyService.getTcCompanyById(id);
        if(null==tcCompany){
            return ResultEntity.error("获取主体明细信息失败");
        }
        return ResultEntity.success(tcCompany);
    }
    @GetMapping("dimension")
    private ResultEntity dimension(@RequestParam(required = true) Long id,String lngLat ){
        boolean flag= tcCompanyService.dimension(id,lngLat);
        if(!flag){
            return ResultEntity.error("主体标注失败");
        }
        return ResultEntity.success();
    }
    @GetMapping("updateCompanyPortrait")
    private ResultEntity updateCompanyPortrait(UpdateCompanyPortraitParams params){
        boolean flag= tcCompanyService.updateCompanyPortrait(params);
        if(!flag){
            return ResultEntity.error("主体画像修改失败");
        }
        return ResultEntity.success();
    }



    /**
     * 修改企业档案
     * @param companyId 企业id
     * @param fieldName 字段名称
     * @param fieldValue 字段值
     * @return
     */
    @RequiresPermissions("company:archives:edit")
    @RequestMapping(value = "/saveCompanyInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity saveCompanyInfo( Long companyId, String fieldName,String fieldValue  ){
        try {
            if(companyId == null){
                return ResultEntity.error("企业id不能为空");
            }
            Assert.notBlank(fieldName, "字段名称不能为空");
            Assert.notBlank(fieldValue, "字段值不能为空");
        } catch (IllegalArgumentException e) {
           return ResultEntity.error(e.getMessage());
        }
        return tcCompanyService.saveUpdateInfo(companyId, fieldName,fieldValue)? ResultEntity.success():ResultEntity.error("修改失败");
    }
}
